
import twitter4j.ResponseList;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_twitter implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);

		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}

		if (extra.length != 0) {
			if (extra[0] != " ") {
				String tosend = null;

				Twitter twitter = new TwitterFactory().getInstance();
				try {
					ResponseList<twitter4j.Status> statuses;
					String user;

					user = extra[0];
					statuses = twitter.getUserTimeline(user);

					System.out
							.println("Showing @" + user + "'s user timeline.");
					int k = 0;
					while (k != 1) {
						for (twitter4j.Status status : statuses) {
							if (k != 1) {
								tosend = "@" + status.getUser().getScreenName()
										+ " - " + status.getText();
							}
							k = 1;
						}
					}
				} catch (TwitterException te) {

					tosend = "Failed to get timeline: " + te.getMessage();

				}

				// address.getCanonicalHostName()
				// tosend = " seems to be up.";

			if(!tosend.contains("Failed to get timeline")){
				irc.sendMessage(channel, start + "" + tosend);
			}else{
				irc.sendMessage(channel, start + "Invalid user!");
			}
			} else {
				
				irc.sendMessage(channel, start + "You need to specify a user!");
			}
		} else {
			
			irc.sendMessage(channel, start + "You need to specify a user!");
		}
	}

	public String help() {
		return "Twitter - twitter [username] - Gets the latest tweet from the user.";
		// TODO Auto-generated method stub

	}

	// public static boolean exists(String URLName){
	// try {
	// HttpURLConnection.setFollowRedirects(true);
	// // note : you may also need
	// //HttpURLConnection.setInstanceFollowRedirects(false)
	// HttpURLConnection con =
	// (HttpURLConnection) new URL(URLName).openConnection();
	// con.setRequestMethod("HEAD");
	// return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
	// }
	// catch (Exception e) {
	// e.printStackTrace();
	// return false;
	// }
	// }
	public String version() {
		return "Haspaid version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "twit, t, twitget";
	}

}
